package com.seata.example.orders.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seata.example.api.InventoryDubboService;
import com.seata.example.orders.entity.Orders;
import com.seata.example.orders.mapper.OrdersMapper;
import com.seata.example.orders.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrderService {
    @DubboReference
    private InventoryDubboService inventoryDubboService;

    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public boolean createOrder(Long id, Orders orders) {
        save(orders);
        inventoryDubboService.reduceInventory(id, orders.getNumber());
        if (id % 2 == 0) {
            throw new RuntimeException();
        }
        return true;
    }
}
